import { toJS } from 'mobx';
import { observable, action } from 'mobx';
import { WeaTableNew, WeaForm } from 'comsMobx';
import { WeaTools } from 'ecCom';
import { Modal, message } from 'antd';
import * as Apis from '../apis/main';

import { WeaLocaleProvider } from 'ecCom';

const getLabel = WeaLocaleProvider.getLabel;

export class CrmPasswordStore {
    @observable initResponsed = false;// 是否已经得到初始化数据的响应
    @observable initSuccessed = false;// 初始化数据是否获取成功
    @observable initFailedMsg = ''; // 初始化处理失败的原因
    @observable loading = false;
    @observable passwordForm = {};
    @observable passwordCondition = [];

    @action
    doInit=(params = {}) => {
      // console.log("doInit in crmPasswordStore");
      Apis.getPasswordForm(params).then((data) => {
        this.doInitCallBack(params, data);
      });
    }

    @action
    doInitCallBack=(params, data) => {
      this.initResponsed = true;
      if (data.status == 'success') {
        this.initSuccessed = true;
        this.passwordForm = new WeaForm();
        this.passwordCondition = data.datas;
        this.passwordForm && !this.passwordForm.isFormInit && this.passwordForm.initFormFields(data.datas);
      } else {
        this.initSuccessed = false;
        this.initFailedMsg = data.msgcode;
      }
    }

    @action
    doClear = () => {
      // console.log("clearStatus in crmPasswordStore");
      this.initResponsed = false;
      this.initSuccessed = false;
      this.initFailedMsg = '';
      this.loading = false;
      this.passwordForm = {};
      this.passwordCondition = [];
    }

    @action
    savePassword = (params = {}) => {
      const that = this;
      const formParams = this.passwordForm.getFormParams();
      const newParams = { ...formParams, ...params };
      this.passwordForm.validate().then((f) => {
        if (f.isValid) {
          if (newParams.passwordnew.length < 6 || newParams.passwordnew.length > 20) {
            Modal.warning({
              title: getLabel(15172, '系统提示'),
              content: getLabel(31862, '密码最少输入6位，最多20位'),
              okText: getLabel(33703, '确定'),
            });
            return false;
          }
          if (newParams.passwordnew == newParams.confirmpassword) {
            const complexity = /^[0-9a-zA-Z]+$/;
            if (!complexity.test(newParams.passwordnew)) {
              Modal.warning({
                title: getLabel(15172, '系统提示'),
                content: getLabel(31863, '新密码不符合要求，必须为字母大写、字母小写、数字组合！请重新输入！'),
                okText: getLabel(33703, '确定'),
              });
              return false;
            }
            that.loading = true;
            Apis.savePassword(newParams).then((data) => {
              that.loading = false;
              if (data.status == 'success') {
                message.success(getLabel(83551, '保存成功！'));
              } else {
                message.error(getLabel(83437, '出现错误！') + data.msgcode);
              }
            });
          } else {
            Modal.warning({
              title: getLabel(15172, '系统提示'),
              content: getLabel(125288, '两次密码输入不一致'),
              okText: getLabel(33703, '确定'),
            });
          }
        } else {
          f.showErrors();
          Modal.warning({
            title: getLabel(15172, '系统提示'),
            content: getLabel(130565, '必要信息不完整，红色标记为必填项！'),
            okText: getLabel(33703, '确定'),
          });
        }
      });
    }
}
